﻿using FlexFlow.Model;
using FlexFlow.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FlexFlow.MVC
{
    /// <summary>
    /// 为工作流表单页面提供基础支持
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class WorkFlowFormPage<T> : WebViewPage<T>
    {
        /// <summary>
        /// 获取当前登录的用户
        /// </summary>
        public new LoginedUserInfo User { get { return DefaultComponentSettings.OnlineUserPool.GetLoginedUserInfo(Request); } }
        protected TVal GetFormValue<TVal>(string key,TVal defaultVal)
        {
            if (Model is WorkFlowForm)
            {
                WorkFlowForm form = Model as WorkFlowForm;
                if (form.Content == null || !form.Content.ContainsKey(key))
                {
                    return defaultVal;
                }
                return (TVal)form.Content[key];
            }
            else
            {
                return defaultVal;
            }
        }
        /// <summary>
        /// 构建一个输入框
        /// </summary>
        public void Input(string name, string defaultText = "")
        {
            string text = GetFormValue(name,defaultText);
            if(this.ViewBag.ReadOnly == null || this.ViewBag.ReadOnly == false)
            {
                ViewContext.Writer.WriteLine("<input type=text style='width:100%' name = '" + name + "' value = '" + text + "'/>");
            }
            else
            {
                ViewContext.Writer.WriteLine("<input type=text readonly style='width:100%' name = '" + name + "' value = '" + text + "'/>");
            }
        }
        /// <summary>
        /// 取得当前登录用户的用户名
        /// </summary>
        public void UserName()
        {
            ViewContext.Writer.WriteLine(User.Name);
        }

    }
}